package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.EmployeeQuery;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import com.sun.tools.internal.xjc.reader.gbind.ElementSets;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    IEmployeeService employeeService;

    @RequestMapping("view")
    @RequiresPermissions(value={"employee:view","员工页面"},logical= Logical.OR)
    public String view(){
        return "employee";
    }


    @RequestMapping("query")
    @ResponseBody
    @RequiresPermissions(value={"employee:query","员工列表"},logical= Logical.OR)
    public PageResult query(EmployeeQuery qo){
        //判断是否市场部经理
        Subject subject = SecurityUtils.getSubject();
        if(subject.hasRole("MANAGER")) {
            //查询全部客户数据
        }else{
            //只查询自己的客户
        }

        return employeeService.query(qo);
    }

    @RequestMapping("validateName")
    @ResponseBody
    public Boolean validateName(String username,Long id){
        return employeeService.validateName(username,id);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value={"employee:saveOrUpdate","员工新增/编辑"},logical= Logical.OR)
    public JSONResult saveOrUpdate(Employee employee){
        try {
            if(employee.getId()!=null){
                employeeService.updateByPrimaryKey(employee);
            }else{
                employee.setState(true);
                employeeService.insert(employee);
            }
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("保存失败!");
        }

    }

    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions(value={"employee:changeState","员工离职"},logical= Logical.OR)
    public JSONResult changeState(Long id){
        try {
            employeeService.changeState(id);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("操作失败!");
        }

    }

    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //设置下载响应头
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        //创建excel文件
        Workbook wb = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = wb.createSheet("day01");
        //查询员工数据
        List<Employee> employees = employeeService.selectAll();
        //创建第一行
        Row row = sheet.createRow(0);
        //设置标题
        row.createCell(0).setCellValue("用户名");
        row.createCell(1).setCellValue("真实姓名");
        row.createCell(2).setCellValue("电话");
        for (int i = 0; i < employees.size(); i++) {
            //从第二行开始创建行
            row = sheet.createRow(i+1);
            Employee employee = employees.get(i);
            //创建单元格
            row.createCell(0).setCellValue(employee.getUsername());
            row.createCell(1).setCellValue(employee.getRealname());
            row.createCell(2).setCellValue(employee.getTel());
        }
        //输出到浏览器
        wb.write(response.getOutputStream());
    }


    @RequestMapping("importXls")
    public void importXls(MultipartFile file) throws IOException {
        //获取excel文件
        Workbook wb = new HSSFWorkbook(file.getInputStream());
        //获取工作簿
        Sheet sheet = wb.getSheet("day01");
        //获取总行数
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i <= lastRowNum ; i++) {
            //获取每一行
            Row row = sheet.getRow(i);
            Employee employee = new Employee();
            employee.setUsername(row.getCell(0).getStringCellValue());
            employee.setRealname(row.getCell(1).getStringCellValue());
            employee.setTel(row.getCell(2).getStringCellValue());
            employee.setPassword("1");
            employeeService.insert(employee);
        }
    }

}

